/**
 * 作者：陈文斌 on 2019/1/22.
 * 邮箱：chen_wennbin@163.com
 * 版本：v1.0
 */

import 'package:flutter/material.dart';
import 'package:crm/model/main_scoped_model.dart';
import 'package:crm/model/Feedback/feedback_model.dart';
import 'package:crm/viewModel/Feedback/feedback_view_model.dart';
import 'package:crm/view/Refresh/refresh_footer_normal.dart';
import 'package:crm/component/Feedback/user_feedback_record_list_row_section.dart';

class UserFeedbackRecordListSection extends StatefulWidget {
  final String customerId;

  UserFeedbackRecordListSection({
    Key key,
    this.customerId,
  }) : super(key: key);

  @override
  State<StatefulWidget> createState() {
    return _UserFeedbackRecordListSectionState();
  }
}

class _UserFeedbackRecordListSectionState
    extends State<UserFeedbackRecordListSection> {
  ScrollController _listController = ScrollController();
  List<FeedbackModel> _feedbackRecords = List<FeedbackModel>();
  bool _isPerformingRequest = false;
  int _page = 0;
  bool _isMore = true;

  _get(page) async {
    if (!_isPerformingRequest) {
      _isPerformingRequest = true;
      FeedbackRecordVM().searchFeedbackRecord(
        page,
        _feedbackRecords,
        customerId: widget.customerId,
        callback: (feedbackRecords, bool isMore) {
          // 请求成功
          _isMore = isMore;
          _isPerformingRequest = false;
          _feedbackRecords = feedbackRecords;

          if (!mounted) return;
          setState(() {});
        },
        errorCallback: (errMsg) => print(errMsg),
      );
    }
  }

  @override
  void initState() {
    super.initState();
    _get(_page);
    _listController.addListener(() {
      if (_listController.position.pixels ==
          _listController.position.maxScrollExtent) _get(++_page);
    });
  }

  @override
  void dispose() {
    super.dispose();
    _listController.dispose();
  }

  @override
  Widget build(BuildContext context) {
    final clientManagerScopedModel = MainModel().of(context);
    return RefreshIndicator(
      displacement: 20.0,
      child: createListView(clientManagerScopedModel),
      onRefresh: () async {
        _page = 0;
        _get(_page);
      },
    );
  }

  Widget createListView(MainModel clientManagerScopedModel) {
    return ListView.builder(
      controller: _listController,
      physics: const AlwaysScrollableScrollPhysics(),
      itemCount: _feedbackRecords.length + 1,
      itemBuilder: (context, i) {
        if (i < _feedbackRecords.length) {
          FeedbackModel _feedbackRecord = _feedbackRecords[i];
          return Column(
            children: <Widget>[
              Padding(padding: EdgeInsets.only(top: 10)),
              UserFeedbackRecordListRowSection(
                feedbackRecord: _feedbackRecord,
                onTap: () {
//                  Navigator.push(
//                    context,
//                    MaterialPageRoute(
//                      builder: (context) => VisitorRecordDetailPage(),
//                    ),
//                  );
                },
              ),
            ],
          );
        } else {
          return RefreshFooterNormal(isMore: _isMore);
        }
      },
    );
  }
}
